//
//  HMMainController.m
//  3-15-网易界面（自定义多控制器）
//
//  Created by apple on 15/3/15.
//  Copyright (c) 2015年 itcast. All rights reserved.
//

#import "HMMainController.h"
#import "HMHomeController.h"
#import "HMHomeLabel.h"
#import "UIView+HMExtension.h"

const int btnCount = 7;

@interface HMMainController ()<UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UIScrollView *topView;
@property (weak, nonatomic) IBOutlet UIScrollView *contentView;

@end

@implementation HMMainController

- (void)viewDidLoad {
    [super viewDidLoad];

    // 1.添加所有的子控制器
    [self setupAllChildVces];
    
    //添加顶部按钮
    [self setupTopView];
    
    //默认设置
    [self setupDefaultSetting];
}

/**
 *  默认设置
 */
- (void)setupDefaultSetting
{
    // 默认情况下，所有控制器view.autoresizingMask中都包含了UIViewAutoresizingFlexibleWidth和UIViewAutoresizingFlexibleHeight两个值
    // 默认情况下，所有控制器view的宽度和高度都是自动伸缩的（跟随父控件伸缩）
    
    // 1.显示第一个控制器
    UIView *firstView = [[self.childViewControllers firstObject] view];
    firstView.frame = self.contentView.bounds;
    [self.contentView addSubview:firstView];
    
    // 2.取出第一个label
    HMHomeLabel *firstLabel = [self.topView.subviews firstObject];
    firstLabel.scale = 1.0;
}


/**
 *  添加所有的子控制器
 */
- (void)setupAllChildVces
{
    for (int i = 0; i<btnCount; i++) {
        HMHomeController *headline = [[HMHomeController alloc] init];
        headline.title = [NSString stringWithFormat:@"频道-%d", i];
        [self addChildViewController:headline];
    }
    
    // 设置底部scrollView的内容大小
    self.contentView.contentSize = CGSizeMake(btnCount * [UIScreen mainScreen].bounds.size.width, 0);
}

/**
 *  添加顶部按钮
 */
- (void)setupTopView
{
    //往顶部scrollView上添加按钮
    CGFloat labelW = 100;
    
    // 添加所有的label
    for (int i = 0; i<btnCount; i++) {
        HMHomeLabel *label = [[HMHomeLabel alloc] init];
        label.tag = i;
        label.text = [self.childViewControllers[i] title];
        [label addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelClick:)]];
        
        label.y = 0;
        label.x = i * labelW;
        label.height = self.topView.frame.size.height;
        label.width = labelW;
        [self.topView addSubview:label];
    }
    
    // 设置内容的尺寸
    self.topView.contentSize = CGSizeMake(btnCount * labelW, 0);
    self.topView.showsHorizontalScrollIndicator = NO;
}

/**
 *  顶部按钮点击事件
 */
- (void)labelClick:(UITapGestureRecognizer *)tap
{
//    NSLog(@"%@",self.childViewControllers);
    // 获得被点击的label
    UIView *label = tap.view;
    
    // 滚动contentsScrollView到当前显示的控制器
    CGFloat contentsOffsetX = label.tag * self.contentView.width;
    CGPoint contentsOffset = CGPointMake(contentsOffsetX, self.contentView.contentOffset.y);
    [self.contentView setContentOffset:contentsOffset animated:YES];
}

#pragma mark - <UIScrollViewDelegate>
/**
 *  只要scrollView在滚动，就会调用
 */
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat value = ABS(scrollView.contentOffset.x / scrollView.width);
    
    // 左边界面的索引
    int leftIndex = value;
    // 右边界面的索引
    int rightIndex = leftIndex + 1;
    
    // 右边的比例
    CGFloat rightScale = value - leftIndex;
    // 左边的比例
    CGFloat leftScale = 1 - rightScale;
    
    HMHomeLabel *leftLabel = self.topView.subviews[leftIndex];
    leftLabel.scale = leftScale;
    
    if (rightIndex >= btnCount) return;
    HMHomeLabel *rightLabel = self.topView.subviews[rightIndex];
    rightLabel.scale = rightScale;
}

/**
 *  当scrollView停止滚动（减速完毕）就会调用(需要通过代码设置滚动才会调用)
 */
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    [self scrollViewDidEndDecelerating:scrollView];
}

/**
 *  当scrollView停止滚动（减速完毕）就会调用(要人为控制滚动才会调用)
 */
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    // 根据偏移量计算索引
    int index = scrollView.contentOffset.x / scrollView.width;
    
    // 让顶部的scrollView滚动
    HMHomeLabel *label = self.topView.subviews[index];
    CGFloat titlesOffsetX = label.center.x - scrollView.width * 0.5;
    // 对顶部scrollView越界处理
    CGFloat maxTitlesOffsetX = self.topView.contentSize.width - self.topView.width;
    if (titlesOffsetX < 0) {
        titlesOffsetX = 0;
    } else if (titlesOffsetX > maxTitlesOffsetX) {
        titlesOffsetX = maxTitlesOffsetX;
    }
    CGPoint titlesOffset = CGPointMake(titlesOffsetX, self.topView.contentOffset.y);
    [self.topView setContentOffset:titlesOffset animated:YES];
    
    // 拿到其他label，scale值清零
    for (HMHomeLabel *otherLabel in self.topView.subviews) {
        if (label != otherLabel) otherLabel.scale = 0.0;
    }
    
    // 添加对应控制器的view到屏幕上
    UIViewController *vc = self.childViewControllers[index];
    // 如果vc的view已经显示在屏幕上，就没必要添加
    if (vc.view.superview) return;
    
    // 添加子控制器的view
    vc.view.width = self.contentView.width;
    vc.view.height = self.contentView.height;
    vc.view.y = 0;
    vc.view.x = index * vc.view.width;
    [self.contentView addSubview:vc.view];
}

@end
